package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.base.BuildTree;
import com.deuwise.common.base.Tree;
import com.deuwise.system.entity.DeptDO;
import com.deuwise.system.service.DeptDOService;
import com.deuwise.system.mapper.DeptDoMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 部门 接口实现类
 *@author deuwise
 *@date 2018-11-09 10:29:13
 **/
@Service
public class DeptDoServiceImpl extends ServiceImpl<DeptDoMapper, DeptDO> implements DeptDOService {

    Logger logger= LoggerFactory.getLogger(DeptDoServiceImpl.class);

    @Override
    public List<DeptDO> queryListParentId(String deptId) {
        Wrapper<DeptDO> wrapper=new EntityWrapper<>();
        wrapper.eq("parent_id",deptId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<DeptDO> getAllDept(List<DeptDO> allList,String deptId){
        List<DeptDO> deptDOList=new ArrayList<>();
        List<DeptDO> childList=   allList.stream().filter(f->f.getParentId().equals(deptId)).collect(Collectors.toList());
        deptDOList.addAll(childList);
        if(childList!=null&&childList.size()>0){
            for(DeptDO deptDO:childList){
                deptDOList.addAll(getAllDept(allList,deptDO.getId()));
            }
        }
        return deptDOList;

    }


    @Override
    public Tree<DeptDO> getTree(DeptDO deptDO) {
        List<Tree<DeptDO>> trees = new ArrayList<Tree<DeptDO>>();
        Wrapper<DeptDO> wrapper=new EntityWrapper<>();
        wrapper.orderBy("order_num",false).orderBy("id",false);
        wrapper.eq("deleted",0);
        if(StringUtils.isNotBlank(deptDO.getName())){
            wrapper.like("name",deptDO.getName());
        }
        List<DeptDO> sysDepts=null;
        if(StringUtils.isNotBlank(deptDO.getParentId())){
            wrapper=new EntityWrapper<>();
            wrapper.eq("deleted",0);
            List<DeptDO> allList= baseMapper.selectList(wrapper);
            Optional<DeptDO> deptD1=allList.stream().filter(f->f.getId().equals(deptDO.getParentId())).findFirst();
            if(deptD1.isPresent()) {
                sysDepts = getAllDept(allList, deptDO.getParentId());
                sysDepts.add(deptD1.get());
                if (StringUtils.isNotBlank(deptDO.getName())) {
                    if (sysDepts != null && sysDepts.size() > 0) {
                        sysDepts = sysDepts.stream().filter(f -> f.getName().contains(deptDO.getName())).collect(Collectors.toList());

                    }
                }
            }
        }else {
            sysDepts = baseMapper.selectList(wrapper);
        }
        for (DeptDO sysDept : sysDepts) {
            Tree<DeptDO> tree = new Tree<DeptDO>();
            tree.setId(sysDept.getId().toString());
            if(StringUtils.isNotBlank(sysDept.getParentId())) {
                tree.setParentId(sysDept.getParentId().toString());
            }else {
                tree.setParentId("");
            }
            Map<String, Object> map=new HashMap<>(16);
            map.put("order_num",sysDept.getOrderNum());
            map.put("deleted",sysDept.getDeleted());
           // map.put("userId",sysDept.getUserId());
            tree.setAttributes(map);
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<DeptDO> t = BuildTree.build(trees);
        return t;
    }

    @Override
    public Integer queryByName(String name, DeptDO deptDO) {
        Wrapper<DeptDO> taskDoWrapper=new EntityWrapper<>();
        taskDoWrapper.eq("name",name);
        if(deptDO!=null){
            if(StringUtils.isNotBlank(deptDO.getId())) {
                taskDoWrapper.ne("id", deptDO.getId());
            }
            if(StringUtils.isNotBlank(deptDO.getParentId())) {
                taskDoWrapper.eq("parent_id", deptDO.getParentId());
            }

        }
        return baseMapper.selectCount(taskDoWrapper);
    }
}